package org.example.calc;

public class EvalVisitor extends MyExprBaseVisitor<Integer>{

    @Override
    public Integer visitAssign(MyExprParser.AssignContext ctx) {
        String id = ctx.ID().getText();
        int value = visit(ctx.expr());
        return value;
    }

    @Override
    public Integer visitInt(MyExprParser.IntContext ctx) {
        return Integer.valueOf(ctx.INT().getText());
    }

    @Override
    public Integer visitPrintExpr(MyExprParser.PrintExprContext ctx) {
        System.out.println("visit:"+ctx.expr().getText());
        return visit(ctx.expr());
    }

    @Override
    public Integer visitParens(MyExprParser.ParensContext ctx) {
        return visit(ctx.expr());
    }

    @Override
    public Integer visitAddsub(MyExprParser.AddsubContext ctx) {
        int left = visit(ctx.expr(0));
        int right = visit(ctx.expr(1));
        if(ctx.op.getType()==MyExprParser.ADD)
        {
            return left+right;

        }
        else {
            return left-right;

        }

    }
}
